Thread: Problem with Adding Vectors using arrays

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    Problem with Adding Vectors using arrays

    Code:
    #include <stdio.h>
    
    int main()
    {
    	long vect1[2] = {0};
    	long vect2[2] = {0};
    	long vect3[2];
    
    	printf("Vector Addition\n\n");
    	printf("Definitions of vectors using standard form:\n");
    	printf("Vector 1-Input the scalars of i, j, and k:\n");
    	scanf("%ld %ld %ld", &vect1[0], &vect1[1], &vect1[2]);
    	
    	printf("Vector 2-Input the scalars of i, j, and k:\n");
    	scanf("%ld %ld %ld", &vect2[0], &vect2[1], &vect2[2]);
    	
    	vect3[0] = vect1[0] + vect2[0];
    	vect3[1] = vect1[1] + vect2[1];
    	vect3[2] = vect1[2] + vect2[2];
    
    
    	printf("\nThe resulting vector is:\n");
    	printf("%ldi + %ldj + %ldk\n\n", vect3[0], vect3[1], vect3[2]);
    	return 0;
    }
    Ok, so here's my problem. This is intended to be a function within a larger program, but before I can nest it within my menu program I need some help. After I receive the input for vect1, everything is fine, but I can't seem to figure out why after I receive input for vect2 that my vect3[0] value seems to copy the vect3[1] value. any ideas on what I'm doing wrong?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You don't want %ld with integers... just use %d ...

    Also put a single getchar() call after your first scanf(), it's probably leaving the newline (Enter key) in the buffer.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    heh, not exactly sure what possessed me to use long instead of int. just making that simple switch was enough to correct the problem. Thanks for the quick reply and help!

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Lonisher View Post
    Ok, so here's my problem. This is intended to be a function within a larger program, but before I can nest it within my menu program I need some help. After I receive the input for vect1, everything is fine, but I can't seem to figure out why after I receive input for vect2 that my vect3[0] value seems to copy the vect3[1] value. any ideas on what I'm doing wrong?
    You are over running the bounds of your arrays and as such are seeing undefined behavior. In C you get what you ask for, so your declared arrays only have 2 elements, accessed by vect[0] and vect[1]. If you want 3 elements you need to declare all your arrays as long vect[3].
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Lonisher View Post
    heh, not exactly sure what possessed me to use long instead of int. just making that simple switch was enough to correct the problem. Thanks for the quick reply and help!
    long, generaly is an abbreviation for long int ... 32 bits on most machines. The %ld formatter is for long long ints which are usualy 64 bits.

    As they say... "This ain't that"...

    You're welcome.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    You are over running the bounds of your arrays and as such are seeing undefined behavior. In C you get what you ask for, so your declared arrays only have 2 elements, accessed by vect[0] and vect[1]. If you want 3 elements you need to declare all your arrays as long vect[3].
    LOL... He is so! Nice catch...

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    In C you get what you ask for, so your declared arrays only have 2 elements, accessed by vect[0] and vect[1]. If you want 3 elements you need to declare all your arrays as long vect[3].
    But I thought arrays defined included the 0 location as an element? Like array[4] would have elements array[0], array[1], array[2], array[3], array[4]

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Nope. array[4] has 4 elements. They go from 0 to 3. Reread the array chapter of your book, carefully.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Will do. Seems as though I've got a lot of brushing up to do.

    Thanks Folks.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Lonisher View Post
    But I thought arrays defined included the 0 location as an element? Like array[4] would have elements array[0], array[1], array[2], array[3], array[4]
    Ummmm, no... an array of four elements has valid indexes of 0, 1, 2 and 3... go ahead count them...

    A common mistake is to confuse "the number of elements" with "the number of the element"...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors in Arrays
    By Osiris990 in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2008, 02:48 PM
  2. Adding Vectors
    By Tonto in forum Game Programming
    Replies: 5
    Last Post: 11-20-2006, 01:06 AM
  3. Adding vectors ??
    By Hussain Hani in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2006, 03:03 AM
  4. adding vectors
    By sdchem in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2003, 11:34 AM
  5. arrays and vectors
    By volk in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2003, 03:45 PM